package edu.towson.forensics;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class LogDBHelper  extends SQLiteOpenHelper {

	/** The Constant TAG. */
	private static final String TAG = LogDBHelper.class.getSimpleName();
	  
	  /** The Constant DB_NAME. */
	  public static final String DB_NAME = "logCatch.db";
	  
	  /** The Constant DB_VERSION. */
	  private static final int DB_VERSION = 1;
	  
	  public static final String T_LOG = "catchedLog";
	  public static final String KEY_ID = "_id";
	  public static final String KEY_LOG = "logline";
	  public static final String KEY_TIME = "catchTime";
	  
  	  //SQLite does not have a storage class set aside for storing dates and/or times
  	  private static final String CREATE_LOG = "CREATE TABLE " + T_LOG 
  	  	+ " (" + KEY_ID + " INTEGER PRIMARY KEY NOT NULL , "
  	  	+ KEY_LOG + " TEXT NOT NULL , "
  	  	+ KEY_TIME + " TEXT);";

  	  /**
		 * The default constructor.
		 * @param context the context
		 */
		public LogDBHelper(Context context) {
	          super(context, DB_NAME, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}



  /**
   * On create.
   * Create database when it is not exist
   * @param db the db
   * {@inheritDoc}
   */
  @Override
  public void onCreate(SQLiteDatabase db) {
//	  MyLog.v(TAG, "onCreate()");
	  
                    db.execSQL(CREATE_LOG);
  }

  /**
   * On upgrade.
   * Update database when it is out of version
   * @param db the db
   * @param oldVersion the old version
   * @param newVersion the new version
   * {@inheritDoc}
   */
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//      MyLog.v(TAG, "onUpgrade(%s, %s)", oldVersion, newVersion);
      // DO NOTHING   
      db.execSQL("DROP TABLE IF EXISTS " + T_LOG + ";");
      onCreate(db);
  }

  public void insertLog(String log){
	  SQLiteDatabase db = this.getWritableDatabase();
	  db.execSQL("INSERT INTO " + T_LOG + "VALUES(NULL, ?,  strftime('%Y-%m-%d %H:%M:%f', 'now'))", new String[]{log} );
  }
  
}


